package ConwaysGameOfLife;

import java.awt.Component;
import java.awt.Point;
import javax.swing.JButton;

/**
 *
 * @author Bryan
 */
public class WorldSettings extends javax.swing.JInternalFrame {

    private final WorldControlMediator mediator;

    private int worldWidth;
    private int worldHeight;
    private boolean randomlyInitialize;

    public WorldSettings(WorldControlMediator mediator) {
        this.mediator = mediator;
        initComponents();
        worldWidth = evalWorldSize().x;
        worldHeight = evalWorldSize().y;
        randomlyInitialize = randomInitialization.isSelected();
        worldSizeIndicator.setText(worldWidth + " x " + worldHeight + " Cells");
        cellSpeedSlider.setText("Simulation Speed: " + simulationSpeedSlider.getValue());
        cellDensityLabel.setText("Spawn Density: " + cellDensitySlider.getValue());
        mediator.setWorldSpeedDelay((int) (simulationSpeedSlider.getValue() - 100) * (-1) * 5);
        mediator.setInitializationDensity(cellDensitySlider.getValue());

    }

    public WorldSettings() {
        this.mediator = null;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        worldSizeLabel = new javax.swing.JLabel();
        worldDefault = new javax.swing.JButton();
        worldColor = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        simulationClear = new javax.swing.JButton();
        simulationStart = new javax.swing.JToggleButton();
        worldColorLabel = new javax.swing.JLabel();
        worldSizeIndicator = new javax.swing.JLabel();
        worldSizeCombo = new javax.swing.JComboBox();
        simulationEnd = new javax.swing.JButton();
        randomInitialization = new javax.swing.JCheckBox();
        brushColorButton = new javax.swing.JButton();
        brushColorLabel = new javax.swing.JLabel();
        cellDensitySlider = new javax.swing.JSlider();
        cellDensityLabel = new javax.swing.JLabel();
        simulationSpeedSlider = new javax.swing.JSlider();
        cellSpeedSlider = new javax.swing.JLabel();

        setIconifiable(true);
        setPreferredSize(new java.awt.Dimension(160, 692));

        jPanel5.setPreferredSize(new java.awt.Dimension(158, 692));
        jPanel5.setRequestFocusEnabled(false);

        worldSizeLabel.setText("World Size");

        worldDefault.setText("Default Settings");
        worldDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                worldDefaultActionPerformed(evt);
            }
        });

        worldColor.setBackground(new java.awt.Color(55, 151, 232));
        worldColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                worldColorActionPerformed(evt);
            }
        });

        simulationClear.setText("Clear Simulation");
        simulationClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simulationClearActionPerformed(evt);
            }
        });

        simulationStart.setText("Start Simulation");
        simulationStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simulationStartActionPerformed(evt);
            }
        });

        worldColorLabel.setText("World Color");

        worldSizeIndicator.setText("# x # Cells");

        worldSizeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Extra Small", "Small", "Medium", "Large", "Extra Large" }));
        worldSizeCombo.setSelectedIndex(2);
        worldSizeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                worldSizeComboActionPerformed(evt);
            }
        });

        simulationEnd.setText("End Simulation");
        simulationEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simulationEndActionPerformed(evt);
            }
        });

        randomInitialization.setSelected(true);
        randomInitialization.setText("Random Spawn");
        randomInitialization.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomInitializationActionPerformed(evt);
            }
        });

        brushColorButton.setBackground(new java.awt.Color(0, 102, 102));
        brushColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brushColorButtonActionPerformed(evt);
            }
        });

        brushColorLabel.setText("Cell Color");

        cellDensitySlider.setValue(10);
        cellDensitySlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cellDensitySliderStateChanged(evt);
            }
        });

        cellDensityLabel.setText("Spawn Density: ");

        simulationSpeedSlider.setValue(80);
        simulationSpeedSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                simulationSpeedSliderStateChanged(evt);
            }
        });
        simulationSpeedSlider.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                simulationSpeedSliderPropertyChange(evt);
            }
        });

        cellSpeedSlider.setText("Simulation Speed:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(simulationSpeedSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addComponent(brushColorButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(worldColor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(worldColorLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(worldSizeLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(simulationStart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(simulationClear, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                        .addComponent(worldSizeIndicator, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(worldSizeCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(brushColorLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(worldDefault, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cellDensitySlider, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addComponent(cellDensityLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(simulationEnd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(cellSpeedSlider)
                    .addComponent(randomInitialization))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(worldSizeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(worldSizeIndicator)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(worldSizeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(worldColorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(worldColor, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(brushColorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(brushColorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(randomInitialization)
                .addGap(18, 18, 18)
                .addComponent(cellDensityLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cellDensitySlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(cellSpeedSlider)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simulationSpeedSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(worldDefault, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simulationStart, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simulationClear, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(simulationEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void worldDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_worldDefaultActionPerformed
        worldSizeCombo.setSelectedIndex(2);
        mediator.defaultWorldColors();


    }//GEN-LAST:event_worldDefaultActionPerformed

    private void worldColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_worldColorActionPerformed
        mediator.toggleWorldPallete();
    }//GEN-LAST:event_worldColorActionPerformed

    private void simulationStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simulationStartActionPerformed
       //Responsible for starting the simulation and disabling buttons        
        if (!mediator.isWorldInitialized()) {
            worldSizeCombo.setEnabled(false);
            //  worldColor.setEnabled(false);
            worldDefault.setEnabled(false);
            randomInitialization.setEnabled(false);
            mediator.initializeWorld();
            simulationStart.setText("Pause Simulation");
            cellDensitySlider.setEnabled(false);
            cellDensityLabel.setEnabled(false);
            worldSizeLabel.setEnabled(false);
            worldSizeIndicator.setEnabled(false);
        } else if (mediator.isWorldInitialized() && !mediator.isWorldPaused()) {
            mediator.togglePause();
            simulationStart.setText("Resume Simulation");
        } else if (mediator.isWorldInitialized() && mediator.isWorldPaused()) {
            mediator.togglePause();
            simulationStart.setText("Pause Simulation");
        }


    }//GEN-LAST:event_simulationStartActionPerformed

    private void simulationClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simulationClearActionPerformed
        mediator.clearSimulation();
    }//GEN-LAST:event_simulationClearActionPerformed

    private void worldSizeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_worldSizeComboActionPerformed
        worldWidth = evalWorldSize().x;
        worldHeight = evalWorldSize().y;
        worldSizeIndicator.setText(worldWidth + " x " + worldHeight + " Cells");
    }//GEN-LAST:event_worldSizeComboActionPerformed

    private void simulationEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simulationEndActionPerformed

        worldDefault.setEnabled(true);
        simulationStart.setSelected(false);
        worldSizeCombo.setEnabled(true);
        randomInitialization.setEnabled(true);
        if (randomInitialization.isEnabled()) {
            cellDensitySlider.setEnabled(true);
            cellDensityLabel.setEnabled(true);
        }
        worldSizeLabel.setEnabled(true);
        worldSizeIndicator.setEnabled(true);
        mediator.setWorldPaused(false);
        mediator.setWorldInitialized(false);
        mediator.clearSimulation();
        simulationStart.setText("Start Simulation");
    }//GEN-LAST:event_simulationEndActionPerformed

    private void randomInitializationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomInitializationActionPerformed
        setRandomlyInitialize(randomInitialization.isSelected());
        cellDensitySlider.setEnabled(!cellDensitySlider.isEnabled());
        cellDensityLabel.setEnabled(!cellDensityLabel.isEnabled());
    }//GEN-LAST:event_randomInitializationActionPerformed

    private void brushColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_brushColorButtonActionPerformed
        mediator.toggleBrushPallete();
    }//GEN-LAST:event_brushColorButtonActionPerformed

    private void simulationSpeedSliderPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_simulationSpeedSliderPropertyChange

    }//GEN-LAST:event_simulationSpeedSliderPropertyChange

    private void simulationSpeedSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_simulationSpeedSliderStateChanged
        cellSpeedSlider.setText("Simulation Speed: " + simulationSpeedSlider.getValue());
        mediator.setWorldSpeedDelay((int) (simulationSpeedSlider.getValue() - 100) * (-1) * 5);
    }//GEN-LAST:event_simulationSpeedSliderStateChanged

    private void cellDensitySliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cellDensitySliderStateChanged
        cellDensityLabel.setText("Spawn Density: " + cellDensitySlider.getValue());
        mediator.setInitializationDensity(cellDensitySlider.getValue());
    }//GEN-LAST:event_cellDensitySliderStateChanged

    
    //this method is simply for translating the combo box to world sizes
    //that are of a proper ratio.
    private Point evalWorldSize() {
        Point size = new Point();

        switch (worldSizeCombo.getSelectedIndex()) {
            case 0:
                size.y = 40;
                size.x = 50;
                break;
            case 1:
                size.y = 80;
                size.x = 100;
                break;
            case 2:
                size.y = 200;
                size.x = 250;
                break;
            case 3:
                size.y = 400;
                size.x = 500;
                break;
            case 4:
                size.y = 800;
                size.x = 1000;
                break;
            default:
                size.y = 50;
                size.x = 40;
                break;
        }

        return size;
    }

    public JButton getBrushColorButton() {
        return brushColorButton;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton brushColorButton;
    private javax.swing.JLabel brushColorLabel;
    private javax.swing.JLabel cellDensityLabel;
    private javax.swing.JSlider cellDensitySlider;
    private javax.swing.JLabel cellSpeedSlider;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JCheckBox randomInitialization;
    private javax.swing.JButton simulationClear;
    private javax.swing.JButton simulationEnd;
    private javax.swing.JSlider simulationSpeedSlider;
    private javax.swing.JToggleButton simulationStart;
    private javax.swing.JButton worldColor;
    private javax.swing.JLabel worldColorLabel;
    private javax.swing.JButton worldDefault;
    private javax.swing.JComboBox worldSizeCombo;
    private javax.swing.JLabel worldSizeIndicator;
    private javax.swing.JLabel worldSizeLabel;
    // End of variables declaration//GEN-END:variables

    
    //a few getters and setters from here on
    
    public int getWorldWidth() {
        return worldWidth;
    }

    public int getWorldHeight() {
        return worldHeight;
    }

    public boolean isRandomlyInitialize() {
        return randomlyInitialize;
    }

    public void setRandomlyInitialize(boolean randomlyInitialize) {
        this.randomlyInitialize = randomlyInitialize;
    }

    public JButton getWorldColor() {
        return worldColor;
    }

}
